package my1024.view;

import java.util.Observable;
import java.util.Observer;

import javax.media.j3d.BranchGroup;
import javax.media.j3d.Canvas3D;

import my1024.utils.EventData;
import my1024.utils.EventType;

import com.sun.j3d.utils.universe.SimpleUniverse;

/**
 * minhvv, p18, IFI, TPE 01/2014 - 07/2014
 */

public class RenderingManager implements Observer {
	private static final int MAX_FRAME_RATE = 72;

	private Canvas3D canvas = null;
	private SimpleUniverse universe = null;
	private MainSceneGraph mainScene = null;

	@Override
	public void update(Observable o, Object arg) {
		EventData obj = (EventData) arg;
		EventType eventType = obj.getEventType();
		switch (eventType) {
		case ADD_BOX:
			mainScene.addObj(obj);
			break;
		case MOVE_BOX:
		case CHANGE_BOX:
			mainScene.changeObj(obj);
			break;
		case DELETE_BOX:
			mainScene.deleteObj(obj);
			break;
		case CTRL_RESET_ROTATE:
			mainScene.resetRotate();
			break;
		default:
			break;
		}
	}

	public RenderingManager(Canvas3D canvas) {
		this.canvas = canvas;
		setupSimpleUniverse();
		setupSceneGraph();
	}

	private void setupSimpleUniverse() {
		universe = new SimpleUniverse(canvas);
		universe.getViewingPlatform().setNominalViewingTransform();
		universe.getViewer().getView()
				.setMinimumFrameCycleTime(1000 / MAX_FRAME_RATE);

	}

	private void setupSceneGraph() {
		mainScene = new MainSceneGraph(universe);
		BranchGroup bg = mainScene.getBranchGroup();
		universe.addBranchGraph(bg);
	}

}
